package com.yutel.phone.xplayer.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import org.apache.http.conn.util.InetAddressUtils;

import android.util.Log;

public class CommonUtil {
	private final static String TAG = "CommonUtil";

	public static InetAddress getAddress() {
		try {
			for (Enumeration<NetworkInterface> mEnumeration = NetworkInterface
					.getNetworkInterfaces(); mEnumeration.hasMoreElements();) {
				NetworkInterface intf = mEnumeration.nextElement();
				for (Enumeration<InetAddress> enumIPAddr = intf
						.getInetAddresses(); enumIPAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIPAddr.nextElement();
					// 如果不是回环地址
					if (!inetAddress.isLoopbackAddress()
							&& InetAddressUtils.isIPv4Address(inetAddress
									.getHostAddress())) {
						return inetAddress;
					}
				}
			}
			return null;
		} catch (Exception e) {
			Log.e(TAG, "InetAddress", e);
			return null;
		}
	}

	public static void shell(String cmd) {
		try {
			Log.i(TAG, "start cmd=" + cmd);
			Process exeEcho = Runtime.getRuntime().exec("su");
			exeEcho.getOutputStream().write(cmd.getBytes());
			exeEcho.getOutputStream().flush();
			Log.i(TAG, "finished");
		} catch (Exception e) {
			Log.e(TAG, "shell error!", e);
		}
	}

	public static String int2ip(int ipInt) {
		StringBuilder sb = new StringBuilder();
		sb.append(ipInt & 0xFF).append(".");
		sb.append((ipInt >> 8) & 0xFF).append(".");
		sb.append((ipInt >> 16) & 0xFF).append(".");
		sb.append((ipInt >> 24) & 0xFF);
		return sb.toString();
	}
}
